package com.example.test.controller;

import com.example.test.model.Student;
import com.example.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    StudentService studentService;

    @GetMapping("/students")
    public List<Student> getStudents()
    {return studentService.list();}

    @GetMapping("/student/{id}")
    public Student getStudent(@PathVariable int id) {
        Student student = studentService.getById(id);
        return student;
    }

    @PostMapping("/student")
    public String addStudent(@RequestBody Student student) {
        if (studentService.save(student)) {
            return "插入学生成功!";
            }
        else return "插入学生失败";
    }

    @PutMapping("/student")
    public String updateStudent(@RequestBody Student student) {
        if (studentService.updateById(student)) {
            return "更新用户成功！";
        }
        else return "更新用户失败";
    }

    @DeleteMapping("/student")
    public String deleteStudent(Integer id) {
        if (studentService.removeById(id)){
            return "删除学生成功！";
        }
        else return "删除学生失败！";
    }

}
